package com.liehacker.android.etcs;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.liehacker.android.R;

/***
 * 다른 APP 실행 
 * @author liehacker
 *
 */
public class OtherAppExecute extends Activity{
	
	private String TAG;
	private Intent intent;	
	PackageManager _manager;
	EditText _edt1;
	EditText _edt2;
	EditText _edt3;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		setContentView(R.layout.otherappexecute);
		
		Button btn1 = (Button)findViewById(R.id.btn1);
		Button btn2 = (Button)findViewById(R.id.btn2);
		Button btn3 = (Button)findViewById(R.id.btn3);
		_edt1 = (EditText)findViewById(R.id.EditText01);
		_edt2 = (EditText)findViewById(R.id.EditText02);
		_edt3 = (EditText)findViewById(R.id.EditText03);
		
		
		_manager = getPackageManager();
		
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				OtherAppExecute.this.OtherAppExecutePackageName(_edt1.getText().toString());
			}
			
		});
		
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				OtherAppExecute.this.OtherAppExecuteClassName(_edt1.getText().toString(), _edt2.getText().toString());
				
			}
			
		});
		
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				OtherAppExecute.this.OtherAppExecuteUri(Uri.parse(_edt3.getText().toString()));
			}
			
		});
	}
	
	/***
	 * 
	 * @param strPackageName "com.tving"
	 */	
	public void OtherAppExecutePackageName(String strPackageName) {
		this.TAG = this.getClass().getName().toString();
						
		try {
			//getInstallPackageInfo();
			
			intent = this.getPackageManager().getLaunchIntentForPackage(strPackageName);
			OtherAppExecute.this.startActivity(intent);
		} catch ( Exception ex ) {
			ex.printStackTrace();
		}
	}
	
	/***
	 *  
	 * @param strPackageName "com.tving"
	 * @param strClassName "com.tving.phone.startActivity" - StartClassName
	 */
	public void OtherAppExecuteClassName(String strPackageName, String strClassName) {
		this.TAG = this.getClass().getName().toString();
		
		//tving.com
		intent = this.getPackageManager().getLaunchIntentForPackage(strPackageName);
		intent.setClassName(strPackageName, strClassName);
		startActivity(intent);
	}
	
	/*** 
	 * 
	 * @param strUri
	 */
	public void OtherAppExecuteUri(Uri strUri) {
		this.TAG = this.getClass().getName().toString();
		
		if(!strUri.equals(null) ) {
			intent = new Intent();
			intent.setAction(Intent.ACTION_VIEW);
			intent.setData(strUri);
			startActivity(intent);
		}
	}
	
	
	/***
	 * 머신에 설치된 엑티비티 리턴 
	 * @return PackageInfo
	 */
	public PackageInfo getInstallPackageInfo() throws NameNotFoundException {		
		
		PackageInfo pi = null;
		List<PackageInfo> appInfoList = null;
		try {
			 appInfoList = _manager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
			 for (int j = 0, jend = appInfoList.size(); j < jend ; j++) {
				 pi = appInfoList.get(j);
				 if( pi.packageName == "com.tving" )
				 {
					 Toast.makeText(getApplicationContext(), pi.packageName, Toast.LENGTH_SHORT);
					// break;
				 }
			 }
		}catch ( Exception ex ) {
			ex.printStackTrace();
		}
		
		return pi;
	}
	
	
}
